package com.dome.order.config;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dome.client.config.exception.FeignException;
import com.dome.client.config.result.enums.RtnResultCodeEnums;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * feign异常处理
 * @author:JZ
 * @date:2020/5/16
 */
@Configuration
@Slf4j
public class OrderFeignErrorDecoder implements ErrorDecoder {

    @Override
    public FeignException decode(String s, Response response) {
        FeignException feignException = new FeignException(RtnResultCodeEnums.SERVICE_EXCEPTION.getMsgCN());
        try {
            String errorContent = Util.toString(response.body().asReader());
            if(StringUtils.isBlank(errorContent)){
                return feignException;
            }
            JSONObject jsonObject=JSON.parseObject(errorContent);
            return new FeignException(jsonObject.toJSONString());
        } catch (IOException e) {
            log.error("处理FeignClient 异常错误");
            e.printStackTrace();
        }
        return feignException;
    }

}


